﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Windows;

namespace DynamicUIFramework
{
    public static class XamlHelper
    {
        public static UserControl LoadXaml(string path)
        {
            try
            {
                UserControl obj = null;
                if (Path.IsPathRooted(path) == false)
                    path = Path.Combine(UISetting.BasePath, path);

                using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
                {
                    //ParserContext pc = new ParserContext();
                    //pc.XamlTypeMapper = new XamlTypeMapper(new string[] { });
                    //pc.XamlTypeMapper.AddMappingProcessingInstruction("http://schemas.weihong.com/mc/2011/IDE", "Weihong.Motion.IDE", "Weihong.Motion.IDE");
                    //pc.XamlTypeMapper.AddMappingProcessingInstruction("http://schemas.microsoft.com/expression/2010/interactivity", "System.Windows.Interactivity", "System.Windows.Interactivity");
                    //pc.XamlTypeMapper.AddMappingProcessingInstruction("http://schemas.microsoft.com/expression/2010/interactions", "Microsoft.Expression.Interactivity.Core", "Microsoft.Expression.Interactions");
                    XamlReader xr = new XamlReader();
                    obj = xr.LoadAsync(fs) as UserControl;
                    //obj = XamlReader.Load(fs) as UserControl;
                    obj.Tag = path.Replace(UISetting.BasePath + "\\", string.Empty).Trim();
                }
                return obj;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }
        }

    }
}
